package com.example.demo.arithmetic.algorithm.dynamic_programing;

import java.util.Arrays;

// 动态规划一波带走
//凑够总金额的所有情况中,所需最少硬币数
/*
 面值  0      1       2       3       4       5
 1     0      1       11      111     1111    11111
 2     0      1       2       21      22      221
 5     0      1       2       21      22      5
 总金额 - 类比为背包容量
 硬币面值 - 类比为物品重量
 硬币个数 - 类比为物品价值 固定为1 求价值(个数)最小的
 递推公式
 if(放得下){
    dp[i][j]=dp[i-1][j]
 }else{
        // 放不下 硬币最少
        dp[i][j]=Math.min(dp[i-1][j],dp[i][j-item.weight]+1)

 }
 */
public class Leetcode322 {

    /**
     * @param coins  从大到小 降序排列
     * @param amount
     * @return
     */
    public int coinChange(int[] coins, int amount) {
        Arrays.sort(coins);
        int[][] dp = new int[coins.length][amount + 1];
        int coin0 = coins[0];
        for (int j = 1; j < amount + 1; j++) {
            if (j >= coin0) {
                dp[0][j] = dp[0][j - coin0] + 1;
            } else {
                // amount+1为特殊值,正常情况不会为amount+1
                // 处理 coins=[2] amount=3这种情况
                dp[0][j] = amount + 1;
            }
        }
        printDp(dp);
        for (int i = 1; i < coins.length; i++) {
            int coin = coins[i];
            for (int j = 0; j < amount + 1; j++) {
                if (j >= coin) {
                    dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - coin] + 1);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
            printDp(dp);
        }

        return dp[coins.length - 1][amount] >= amount + 1 ? -1 : dp[coins.length - 1][amount];
    }

    /**
     * 降维
     *
     * @param coins
     * @param amount
     * @return
     */
    public int coinChange2(int[] coins, int amount) {
        Arrays.sort(coins);
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        //        int coin0 = coins[0];
        //        for (int j = 1; j < amount + 1; j++) {
        //            if (j >= coin0) {
        //                dp[j] = dp[j - coin0] + 1;
        //            } else {
        //                // amount+1为特殊值,正常情况不会为amount+1
        //                // 处理 coins=[2] amount=3这种情况
        //                dp[j] = amount + 1;
        //            }
        //        }
        //        System.out.println(Arrays.toString(dp));
        for (int coin : coins) {
            for (int j = coin; j < amount + 1; j++) {
                dp[j] = Math.min(dp[j], dp[j - coin] + 1);
            }
            System.out.println(Arrays.toString(dp));
        }

        return dp[amount] >= amount + 1 ? -1 : dp[amount];
    }

    private void printDp(int[][] arr) {
        for (int[] its : arr) {
            for (int anInt : its) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
//        System.out.println(new Leetcode322().coinChange(new int[]{5, 2, 1}, 5));
        System.out.println(new Leetcode322().coinChange2(new int[]{5, 2, 1}, 5));
//        System.out.println(new Leetcode322().coinChange(new int[]{2}, 3));
    }
}
